﻿using DevelopTool.Bll.DbBlls;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.DbModels;
using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DevelopTool.Forms.DbInfoForms
{
    /// <summary>
    /// Oracle连接信息编辑窗体
    /// </summary>
    public partial class OracleDbInfoForm : BaseUIForm
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;

        private DbInfoBll dbInfoBll;

        private ManageMenuBll manageMenull;

        public OracleDbInfoForm(DbInfo dbInfo) : base()
        {
            InitializeComponent();
            this.dbInfo = dbInfo;
        }


        private void OracleDbInfoForm_Load(object sender, EventArgs e)
        {
            this.dbInfoBll = new DbInfoBll();
            this.manageMenull = new ManageMenuBll();

            if (dbInfo != null && dbInfo.Id > 0)
            {
                TextBoxName.Text = dbInfo.ConnectionString.IsEmpty() ? dbInfo.NodeName : "";
                TextBoxName2.Text = dbInfo.Ip.IsEmpty() ? dbInfo.NodeName : "";
                TextBoxIp.Text = dbInfo.Ip;
                TextBoxUserName.Text = dbInfo.UserName;
                TextBoxPassword.Text = dbInfo.Password;
                TextBoxLinkStr.Text = dbInfo.ConnectionString;
            }

            ThemHelper.SetSystemThem(this);
        }

        /// <summary>
        /// 确定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonOk_Click(object sender, EventArgs e)
        {
            DbInfo dbInfo = GetDbInfo(TabControl.SelectedTab.Text);
            if (dbInfo == null) return;

            //保存至数据库
            if (dbInfo.Id <= 0 && manageMenull.CheckName<DbInfo>(dbInfo.NodeName, 0))
            {
                this.ShowWarningDialog($"已存在连接名：【{dbInfo.NodeName}】，换个名字吧");
                return;
            }

            var result = dbInfoBll.Save(dbInfo);

            if (result.Id > 0)
            {
                this.Tag = dbInfo;
                this.ShowSuccessNotifier("保存成功");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                this.ShowErrorNotifier("保存失败");
            }
        }


        private DbInfo GetDbInfo(string type)
        {
            DbInfo dbInfo = null;
            switch (type)
            {
                case "方式一":

                    #region 数据验证
                    if (!base.CheckData(TextBoxName, "连接名不能为空"))
                    {
                        return null;
                    }
                    else if (!base.CheckData(TextBoxIp, "主机地址不能为空"))
                    {
                        return null;
                    }
                    else if (!base.CheckData(TextBoxUserName, "用户名不能为空"))
                    {
                        return null;
                    }
                    else if (!base.CheckData(TextBoxPassword, "密码不能为空"))
                    {
                        return null;
                    }
                    #endregion 数据验证

                    dbInfo = this.dbInfo ?? new DbInfo();
                    dbInfo.NodeName = TextBoxName.Text;
                    dbInfo.DbType = Model.Enums.DbType.Oracle;
                    dbInfo.Ip = TextBoxIp.Text;
                    dbInfo.UserName = TextBoxUserName.Text;
                    dbInfo.Password = TextBoxPassword.Text;
                    dbInfo.ConnectionString = string.Empty;

                    break;
                case "方式二":

                    #region 数据验证
                    if (!base.CheckData(TextBoxName2, "连接名不能为空"))
                    {
                        return null;
                    }
                    else if (!base.CheckData(TextBoxLinkStr, "连接串不能为空"))
                    {
                        return null;
                    }
                    #endregion 数据验证

                    dbInfo = this.dbInfo ?? new DbInfo();
                    dbInfo.NodeName = TextBoxName2.Text;
                    dbInfo.DbType = Model.Enums.DbType.Oracle;
                    dbInfo.ConnectionString = TextBoxLinkStr.Text;
                    dbInfo.Ip = string.Empty;
                    break;
            }
            return dbInfo;
        }

        /// <summary>
        /// 测试事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonTest_Click(object sender, EventArgs e)
        {

            try
            {
                var tabpage = TabControl.SelectedTab;
                DbInfo dbInfo = GetDbInfo(tabpage.Text);
                if (dbInfo == null) return;

                //数据库连接测试
                OracleDbBll oracleDbBll = new OracleDbBll(dbInfo);
                bool connectOk = false;
                LoadingHelper.ShowLoading("测试连接数据库...", () =>
                {
                    connectOk = oracleDbBll.TestConnect();
                });

                if (!connectOk)
                {
                    this.ShowErrorNotifier($"【{tabpage.Text}】连接失败");
                }
                else
                {
                    this.ShowSuccessNotifier($"【{tabpage.Text}】连接成功");
                }

            }
            catch (Exception ex)
            {
                this.ShowErrorDialog("Oracle连接失败", ex.Message);
            }

        }


        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            base.TextBoxTextChanged(sender, e);
        }
    }
}
